package com.wwt.qiandao.controller;

import com.wwt.qiandao.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.Map;

@Controller
public class QingdaoController {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/index")
    public String indes(){
        return "index";
    }
    @PostMapping("/login")
    public String listUser(User user,Model model){
        List<User> list=redisTemplate.opsForList().range("user_p",0,-1);
        int a= 1;
        for (User u : list){
            if (u.equals(user)){
                System.out.println("登录成功:"+u);
                model.addAttribute("user",u.getName());
                break;
            }
            if (list.size()==a){
                if (u.equals(user)){
                    System.out.println("登录成功:"+u);
                    break;
                }else {
                    System.out.println("登录失败");
                    return "";
                }
            }
            a++;
        }
        if (redisTemplate.opsForList().size("user_b")==0){
            for (User u : list){
                redisTemplate.opsForList().rightPushAll("user_b",u.getName());
            }
        }
        List<String> names=redisTemplate.opsForList().range("user_b",0,-1);
        model.addAttribute("names",names);
        return "qingdao";
    }

    @GetMapping("/qiangdao")
    public String qiangdao(String name,Model model){
        model.addAttribute("user",name);
        redisTemplate.opsForList().remove("user_b",0,name);
        List<String> names=redisTemplate.opsForList().range("user_b",0,-1);
        model.addAttribute("names",names);
        return "qingdao";
    }
}
